<?php

/**
 * Created on Jun 30, 2007
 * API for MediaWiki 1.8+
 *
 * Copyright © 2007 Roan Kattouw <Firstname>.<Lastname>@home.nl
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
 * http://www.gnu.org/copyleft/gpl.html
 */
 
require_once( '/home/cyberpower678/public_html/articleinfo/base.php' );
WebTool::setMemLimit();

/**
 * API module that facilitates deleting pages. The API eqivalent of action=delete.
 * Requires API write mode to be enabled.
 *
 * @ingroup API
 */
class ApiArticleinfo extends ApiBase {

        public function __construct( $main, $action ) {
                parent::__construct( $main, $action );
        
        }

        /**
         * Extracts the title, token, and reason from the request parameters and invokes
         * the local delete() function with these as arguments. It does not make use of
         * the delete function specified by Article.php. If the deletion succeeds, the
         * details of the article deleted and the reason for deletion are added to the
         * result object.
         */
        public function execute() {
                global $lang, $wiki, $url, $site;
                
                $params = $this->extractRequestParams();
                
                if ( !isset( $params['article'] ) ) {
                        $this->dieUsageMsg( array( 'missingparam', 'article' ) );
                }
                $article = trim( str_replace( array('&#39;','%20'), array('\'',' '), $params['article'] ) );
                $article = urldecode($article);
                
                $lang = $params['lang'];
                $wiki = $params['wiki'];
                $url = $lang.'.'.$wiki.'.org';
                
                $ret = WebTool::loadForApi( 'ArticleInfo', false );
                if( isset( $ret['error'] ) ) $this->dieUsage( $ret['info'], $ret['error'] );
                
                try {
                        $pageClass = $site->initPage( $article, null, !$params['nofollowredir'] );
                } catch( BadTitle $e ) {
                        $this->dieUsage( 'The page given doesn\'t exist','nosuchpage' );
                } catch( Exception $e ) {
                        $this->dieUsage( $e->getMessage(), 'unknownerror' );
                }
                
                if( !$pageClass->exists() ) $this->dieUsage( 'The page given doesn\'t exist', 'nosuchpage' );
                
                $history = Base::getVars( 
                        $pageClass, 
                        $site, 
                        $params['nofollowredir'],
                        ( !empty( $params['begin'] ) ) ? $params['begin'] : false, 
                        ( !empty( $params['end'] ) ) ? $params['end'] : false
                );
                if( isset( $history['error'] ) ) $this->dieUsage( $history['info'], $history['error'] );
                if( count( $history ) == 50000 ) $this->setWarning( "Too many revisions in history, showing 50000 of them" );
                
                if( isset( $params['limit'] ) ) {
                        $i = 0;
                        foreach( $history as $count => $nothing ) {
                                if( $i > $params['limit'] ) unset( $history[$count] );
                                $i++;
                        }
                }
        
                $data = Base::parseHistory( 
                        $history, 
                        ( !empty( $params['begin'] ) ) ? $params['begin'] : false, 
                        ( !empty( $params['end'] ) ) ? $params['end'] : false,
                        $site, 
                        $pageClass,
                        true
                );
                
                foreach( $data['year_count'] as $year => &$info ) {
                        if( is_array( $info ) ) {
                                
                                foreach( $info['months'] as $month => &$nothing ) {
                                        
                                        //if( is_array( $nothing ) ) $this->getResult()->setIndexedTagName( $nothing, 'month' );
                                        $nothing['month'] = $month;
                                }
                                
                                $this->getResult()->setIndexedTagName( $info['months'], 'month' );
                                $info['year'] = $year;
                        }
                        
                        
                }

                $i = 0;
                foreach( $data['editors'] as $editor => $info2 ) {
                        $data['editors'][$editor]['name'] = $editor;
                        $data['editors'][$i] = $data['editors'][$editor];
                        unset($data['editors'][$editor]);
                        $i++;
                }
                
                $this->getResult()->setIndexedTagName( $data['year_count'], 'year' );
                $this->getResult()->setIndexedTagName( $data['anons'], 'ip' );
                $this->getResult()->setIndexedTagName( $data['editors'], 'editor' );
                $this->getResult()->setIndexedTagName( $data['top_ten']['editors'], 'editor' );
                $this->getResult()->setIndexedTagName( $data['top_fifty'], 'editor' );
                
                //var_dump($data);
                
                
                $this->getResult()->addValue( 'query', $this->getModuleName(), $data );
        }

        public function getAllowedParams() {
                return array(
                        'article' => null,
                        'lang' => array(
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_DFLT => 'en'
                        ),
                        'wiki' => array(
                                ApiBase::PARAM_TYPE => 'string',
                                ApiBase::PARAM_DFLT => 'wikipedia'
                        ),
                        'begin' => array( ApiBase::PARAM_TYPE => 'timestamp' ),
                        'end' => array( ApiBase::PARAM_TYPE => 'timestamp' ),
                        'nofollowredir' => array(
                                ApiBase::PARAM_TYPE => 'boolean',
                                ApiBase::PARAM_DFLT => false
                        ),
                        'limit' => array(
                                ApiBase::PARAM_TYPE => 'limit',
                                ApiBase::PARAM_MIN => 1,
                                ApiBase::PARAM_MAX => ApiBase::LIMIT_BIG1,
                                ApiBase::PARAM_MAX2 => ApiBase::LIMIT_BIG2 
                        )
                );
        }

        public function getParamDescription() {
                $p = $this->getModulePrefix();
                return array(
                        'article' => "Title of the page to get statistics for",
                        'lang' => "Subdomain of wiki that article is stored on (e.g. en, de, commons)",
                        'wiki' => "Domain of wiki that article is stored on (e.g. wikipedia, wikisource)",
                        'begin' => "Timestamp to start getting history for",
                        'end' => "Timestamp to stop getting history",
                        'nofollowredir' => "Set this to use the given article, even if it redirects to another article",
                        'limit' => "Only analyze this many edits"
                );
        }

        public function getDescription() {
                return 'Get statistics about the history of an article';
        }

        public function getPossibleErrors() {
                return array_merge( parent::getPossibleErrors(), array(
                        array( 'nosuchpage' ),
                        array( 'dberror' ),
                ) );
        }

        protected function getExamples() {
                return array(
                        'api.php?action=articleinfo&article=Foobar',
                        'api.php?action=articleinfo&article=Barack_Obama&limit=50&begin=20070101000000',
                        'api.php?action=articleinfo&article=Accueil&lang=fr'
                );
        }

        public function getVersion() {
        }
}
